package com.lzj.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzj.admin.pojo.DamageList;
import com.lzj.admin.mapper.DamageListMapper;
import com.lzj.admin.pojo.DamageListGoods;
import com.lzj.admin.pojo.Goods;
import com.lzj.admin.query.DamageListQuery;
import com.lzj.admin.service.IDamageListGoodsService;
import com.lzj.admin.service.IDamageListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzj.admin.service.IGoodsService;
import com.lzj.admin.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报损单表 服务实现类
 * </p>
 *
 * @author luoyoumou
 * @date 2022-12-15
 */
@Service
public class DamageListServiceImpl extends ServiceImpl<DamageListMapper, DamageList> implements IDamageListService {

    @Resource
    private IGoodsService goodsService;
    @Resource
    private IDamageListGoodsService damageListGoodsService;

    @Override
    public String getNextDamageNumber() {

        try {
            //定义并初始化报损单号
            StringBuffer sb=new StringBuffer();
            sb.append("BS");
            sb.append(DateUtil.getCurrentDateStr());
            //生成单号后四位，追加
            //在数据库中查询当日最新报损记录
            // 判断是否已有报损记录  截取最后四位 序号递增  没有则从0001开始
            String number=this.baseMapper.getNextDamageNumber();
            if (number != null) {
                sb.append(StringUtil.formatCode(number));
            }else {
                sb.append("0001");
            }
            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }


    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor =Exception.class)
    public void saveDamageList(DamageList damageList, List<DamageListGoods> plgList) {
        //执行添加
        damageList.setCreateDate(new Date());
        AssertUtil.isTrue(!(this.save(damageList)),"记录添加失败!");
        //查询报损单号相同
        DamageList temp = this.getOne(new QueryWrapper<DamageList>().eq("damage_number",damageList.getDamageNumber()));
        //每个报损商品 减库存
        plgList.forEach(plg->{
            AssertUtil.isTrue(null == plg.getNum()|| 1 > plg.getNum(),"请检查：每件商品的 数量 必须 >=1 ！");
            plg.setDamageListId(temp.getId());
            plg.setFpyName(PinYinUtil.getFirstSpell(plg.getName()));
            Goods goods = goodsService.getById(plg.getGoodsId());
            // 检查库存
            AssertUtil.isTrue(goods.getInventoryQuantity()< plg.getNum(),"商品 "+ goods.getName() +" 的库存数量不足，操作失败！");
            goods.setInventoryQuantity(goods.getInventoryQuantity()-plg.getNum());
            goods.setState(2);
            AssertUtil.isTrue(!(goodsService.updateById(goods)),"报损单记录添加失败-@1!");
            AssertUtil.isTrue(!(damageListGoodsService.save(plg)),"报损单记录添加失败-@2!");
        });
    }

    @Override
    public Map<String, Object> damageList(DamageListQuery damageListQuery) {
        IPage<DamageList> page =new Page<DamageList>(damageListQuery.getPage(),damageListQuery.getLimit());
        page=this.baseMapper.damageList(page,damageListQuery);
        return PageResultUtil.getResult(page.getTotal(),page.getRecords());
    }

}
